//
//  WBComposeTextView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/17.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBComposeTextView: UITextView {
    
    // MARK: - 重写font属性 动态给占位label设置字体大小
    override var font: UIFont?{
    
        didSet{
            placeholderLabel.font = font
        }
        
    }
    
    // MARK: - 自定义占位文字属性 动态获取占位文字
    var placeholder: String?{
    
        didSet{
            
            placeholderLabel.text = placeholder
        
        }
    
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置UI
    func setupUI() {
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(placeholderLabel)
        
        //设置约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(ScreenW - 10)
        }
        
        //添加通知 textview文字改变的通知 当文字改变的时候我就要隐藏label
        //一定要记住删除通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(WBComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    // MARK: - 接收到通知以后要做的事情
    func textViewTextDidChange() {
        
        //隐藏占位label
        placeholderLabel.hidden = true
        
    }
    
    // MARK: - 移除通知
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
     // MARK: - 懒加载Label 用来显示占位文字
    private lazy var placeholderLabel: UILabel = {

        let label = UILabel()
        //设置文字颜色
        label.textColor = UIColor.darkGrayColor()
        
        //设置换行
        label.numberOfLines = 0
    
        return label
    }()
    

}
